package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_SET_DEFAULT_SKIN, desc = "设置默认皮肤")
public class SetDefaultSkinCmd extends AbstractCommand {

	private int skinId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.SetDefaultSkinReqMsg reqMsg = PlayerProto.SetDefaultSkinReqMsg.parseFrom(packet.getBytes());
		skinId = reqMsg.getDefaultSkin();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if (!player.getModule(DecorationModule.class).hasDecoration(eDecorationType.Costume.getValue(), skinId)) {
			ret = GameErrorCode.E_DECORATION_NO_FOUND;
			return;
		}
		player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.DefaultSkin, skinId);
	}

	@Override
	public void sendData(GamePlayer player) {
		PlayerProto.SetDefaultSkinRespMsg.Builder respMsg = PlayerProto.SetDefaultSkinRespMsg.newBuilder();
		respMsg.setRet(ret);
		if (0 == ret) {
			respMsg.setDefaultSkin(skinId);
		}
		player.sendPacket(Protocol.U_SET_DEFAULT_SKIN, respMsg);
	}

}
